package com.example.forum.controller;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.config.AppConfig;
import com.example.forum.model.Article;
import com.example.forum.model.ArticleReply;
import com.example.forum.model.User;
import com.example.forum.services.IArticleReplyService;
import com.example.forum.services.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@Slf4j
@RequestMapping(("/reply"))
@Api(tags = "帖子回复接口")
public class ArticleReplyController {
    @Autowired
    private IArticleReplyService articleReplyService;
    @Autowired
    private IArticleService articleService;

    /**
     * 创建帖子回复
     * @param articleId 帖子id
     * @param content 帖子回复正文
     */
    @ApiOperation("创建帖子回复")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam("被回复的id") @RequestParam("articleId") @NonNull Long articleId,
                            @ApiParam("回复正文") @RequestParam("content") @NonNull String content){
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //判断用户是否被禁言
        if(user.getState()==1){
            //表示用户禁言
            log.warn("当前用户已被禁言!无法点赞,userId:{}",user.getId());
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        Article article = articleService.selectById(articleId);
        if(null==article || article.getDeleteState()==1){
            //帖子已被删除
            log.warn("当前帖子不存在!");
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXIST);
        }
        //帖子是否被禁止操作
        if(article.getState()==1){
            log.error("该帖子已被禁止回复!");
            return AppResult.failed(ResultCode.ARTICLE_IS_BANNED);
        }
        ArticleReply articleReply = new ArticleReply();
        articleReply.setArticleId(articleId); //要回复的帖子
        articleReply.setContent(content); //正文
        articleReply.setPostUserId(user.getId()); //回复的发送者
        articleReplyService.create(articleReply);
        log.info("回复帖子成功!");
        return AppResult.success();
    }

    /**
     * 查询帖子回复
     * @param articleId 帖子id
     */
    @ApiOperation("通过帖子id获取所有的回复")
    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getRepliesByArticleId(@ApiParam("帖子id") @RequestParam("articleId") @NonNull Long articleId){
        //查询帖子是否存在
        Article article = articleService.selectById(articleId);
        if(null==article || article.getDeleteState()==1){
            //帖子已被删除
            log.warn("当前帖子不存在!");
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXIST);
        }
        List<ArticleReply> articleReplyList = articleReplyService.selectByArticleId(articleId);

        return AppResult.success(articleReplyList);
    }
}
